﻿// ***********************************************************************
// Assembly         : AccesoDatos
// Author           : Diego
// Created          : 03-02-2014
//
// Last Modified By : Diego
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="AD_DetalleEstructural.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using Modelado.Modulos.ClasesModelado;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// The ClasesAccesoDatos namespace.
/// </summary>
namespace AccesoDatos.Modulos.ClasesAccesoDatos
{
    /// <summary>
    /// Class AD_DetalleEstructural.
    /// </summary>
    public class AD_DetalleEstructural
    {
        /// <summary>
        /// Crs the e_ detalle_ estructural.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="obj">The object.</param>
        public void CRE_Detalle_Estructural(SqlCommand comando, MOD_DetalleEstructural obj)
        {
            try
            {
                string sp = "SP_CRE_Detalle_Estructural";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODORDEN", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_DESCRIPCION", SqlDbType.VarChar));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODSUBTIPEST", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODMAT", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_ANCHOMAT", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_LARGOMAT", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CANTIDADMAT", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODESTRUCTURA", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODCLASUBEST", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_TOTALINEAMAT", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_TIPOCAMBIO", SqlDbType.Decimal));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODSECEST", SqlDbType.Int));

                comando.Parameters[0].Value = obj.DETORDEST_codorden;
                comando.Parameters[1].Value = obj.DETORDEST_descripcion;
                comando.Parameters[2].Value = obj.DETORDEST_codsubtipoest;
                comando.Parameters[3].Value = obj.DETORDEST_codmat;
                comando.Parameters[4].Value = obj.DETORDEST_anchomat;
                comando.Parameters[5].Value = obj.DETORDEST_largomat;
                comando.Parameters[6].Value = obj.DETORDEST_cantidadmat;
                comando.Parameters[7].Value = obj.DETORDEST_codestructura;
                comando.Parameters[8].Value = obj.DETORDEST_codclabsubest;
                comando.Parameters[9].Value = obj.DETORDEST_totallineamat;
                comando.Parameters[10].Value = obj.DETORDEST_tipocambio;
                comando.Parameters[11].Value = obj.DETORDEST_codsecest;
             
               

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Ses the l_ grid_ detalle_ estructural.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codOrden">The cod orden.</param>
        /// <returns>List{MOD_DetalleEstructural}.</returns>
        public List<MOD_DetalleEstructural> SEL_Grid_Detalle_Estructural(SqlCommand comando, int codOrden)
        {
            try
            {
                string sp = "SP_SEL_DetalleEstructuralByOrden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODORDEN", SqlDbType.Int));
                comando.Parameters[0].Value = codOrden;

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                List<MOD_DetalleEstructural> lista = new List<MOD_DetalleEstructural>();

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MOD_DetalleEstructural obj = new MOD_DetalleEstructural();
                    obj.DETORDEST_codlinea = Convert.ToInt32(dt.Rows[i]["DETORDEST_CODLINEA"]);
                    obj.DETORDEST_estructura = dt.Rows[i]["EST_NOMBRE"].ToString();
                    obj.DETORDEST_clabsubest = dt.Rows[i]["CLASUB_NOMBRE"].ToString();
                    obj.DETORDEST_subtipoest = dt.Rows[i]["SUBTIPOEST_NOMBRE"].ToString();
                    obj.DETORDEST_material = dt.Rows[i]["MAT_DESCRIPCION"].ToString();
                    obj.DETORDEST_cantidadmat =Convert.ToInt32( dt.Rows[i]["DETORDEST_CANTIDADMAT"]);
                    obj.DETORDEST_totallineamat =Convert.ToInt32( dt.Rows[i]["DETORDEST_TOTALINEAMAT"]);

                    lista.Add(obj);
                }


                return lista;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Ses the l_ total_ estructural.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codOrden">The cod orden.</param>
        /// <returns>System.Decimal.</returns>
        public decimal SEL_Total_Estructural(SqlCommand comando, int codOrden)
        {
            try
            {
                string sp = "SP_SEL_TotalByOrden";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();
                decimal total_est = 0;
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODORDEN", SqlDbType.Int));
                comando.Parameters[0].Value = codOrden;

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
              

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    total_est = Convert.ToInt32(dt.Rows[i]["TOTAL_ORDEN"]);


                }

                return total_est;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Des the l_ detalle estructural.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="Codigo">The codigo.</param>
        public void DEL_DetalleEstructural(SqlCommand comando, int Codigo)
        {
            try
            {
                string sp = "SP_DEL_DetalleEstructural";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();


                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODLINEA", SqlDbType.Int));

                comando.Parameters[0].Value = Codigo;

                comando.ExecuteNonQuery();


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Ses the l_ detalle estructural by ordenamiento.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codEstructura">The cod estructura.</param>
        /// <param name="codSubEstructura">The cod sub estructura.</param>
        /// <param name="codSubTipoEstructura">The cod sub tipo estructura.</param>
        /// <param name="codMaterial">The cod material.</param>
        /// <param name="codOrden">The cod orden.</param>
        /// <returns>System.Int32.</returns>
        public int SEL_DetalleEstructuralByOrdenamiento(SqlCommand comando, int codEstructura, int codSubEstructura, int codSubTipoEstructura, int codMaterial, int codOrden)
        {
            try
            {
                string sp = "SP_SEL_DetalleEstructuralByOrdenamientol";
                int det_cant = 0;
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODESTRUCTURA", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODCLASUBEST", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODSUBTIPEST", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODMAT", SqlDbType.Int));
                comando.Parameters.Add(new SqlParameter("@DETORDEST_CODORDEN", SqlDbType.Int));

                comando.Parameters[0].Value = codEstructura;
                comando.Parameters[1].Value = codSubEstructura;
                comando.Parameters[2].Value = codSubTipoEstructura;
                comando.Parameters[3].Value = codMaterial;
                comando.Parameters[4].Value = codOrden;



                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    det_cant = Convert.ToInt32(dt.Rows[i]["DET_CANT"]);


                }

                return det_cant;


            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        /// <summary>
        /// Ses the l_ cant_ det est.
        /// </summary>
        /// <param name="comando">The comando.</param>
        /// <param name="codOrden">The cod orden.</param>
        /// <returns>System.Int32.</returns>
        public int SEL_Cant_DetEst(SqlCommand comando, int codOrden)
        {
            try
            {
                string sp = "SP_SEL_DETALLEEST_LINEAByORDEN";

                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = sp;
                comando.Parameters.Clear();

                comando.Parameters.Add(new SqlParameter("@DETEST_CODORDEN", SqlDbType.Int));
                comando.Parameters[0].Value = codOrden;

                IDbDataAdapter da = new SqlDataAdapter((SqlCommand)comando);
                DataSet ds = new DataSet();
                da.Fill(ds);

                DataTable dt = ds.Tables[0];
                int cant_detnoest = 0;

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cant_detnoest = Convert.ToInt32(dt.Rows[i]["CANT_LINEADET"]);

                }


                return cant_detnoest;

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
       
    }
}
